package com.gusti.turnos.services.impl;

import java.util.List;
import java.util.UUID;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gusti.turnos.exception.TurnosException;
import com.gusti.turnos.form.UserForm;
import com.gusti.turnos.model.User;
import com.gusti.turnos.model.UserRol;
import com.gusti.turnos.response.rest.TurnosResponse;
import com.gusti.turnos.services.AbstractService;
import com.gusti.turnos.services.IUserService;
import com.gusti.turnos.util.EmailUtil;
import com.gusti.turnos.util.MessageUtil;

/**
 * 
 * @author gusti
 *
 */
@Service
public class UserService extends AbstractService implements IUserService {

	@Override
	public List<User> retrieveUsers() {

		List<User> users = userDao.retrieveUsers();

		//clean the user password
		for( User user : users ) {
			user.setPassword( null );
		}

		return users;
	}

	@Override
	public User retrieveUser( long id ) {

		User user = userDao.retrieveUser( id );
		//clean the user password
		user.setPassword( null );

		return user;
	}

	@Override
	public TurnosResponse retrieveUsers( String sessionId, String uuid ) {

		checkAuthenticationCredentials( sessionId, uuid );

		List<User> users = retrieveUsers();

		TurnosResponse turnosResponse = new TurnosResponse();
		turnosResponse.setSuccess( true );
		turnosResponse.setData( users );

		return turnosResponse;
	}

	@Override
	@Transactional
	public TurnosResponse saveUser( String sessionId, String uuid, UserForm userForm ) {

		checkAuthenticationCredentials( sessionId, uuid );
		checkUserAdminLevel();

		TurnosResponse turnosResponse = new TurnosResponse();

//		//check unique email
//		if( !userDao.checkUniqueField( User.class.getSimpleName(), "email", userForm.getEmail() ) ) {
//			throw new TurnosException( MessageUtil.getMessage( MessageUtil.ERROR_USER_EMAIL_USED ) );
//		}
//
//		//generate and check unique uuid
//		String generatedUuid = "";
//		do {
//
//			generatedUuid = UUID.randomUUID().toString();
//
//		} while ( !userDao.checkUniqueField( User.class.getSimpleName(), "uuid", generatedUuid ) );
//
//		String decodedPassword = AuthenticationService.generateTemporaryPassword();
//		String encodedPassword = AuthenticationService.encodePassword( decodedPassword );
//
//		//save user
////			user = userDao.save( user );
//		User user = saveUser( userForm, encodedPassword, generatedUuid );
//
//		if( user.isNew() ) {
//			turnosResponse.setErrorMessage( MessageUtil.getMessage( "ERROR_SAVE" ) );
//		} else {
//			turnosResponse.setSuccess( EmailUtil.sendNewUserEmail( user, decodedPassword ) );
//			turnosResponse.setData( user );
//		}

		return turnosResponse;
	}

	@Transactional
	private User saveUser( UserForm userForm, String encodedPassword, String generatedUuid ) {

		User user = new User();
		if( userForm.getId() == 0 ) {
			user.setPassword( encodedPassword );
			user.setUuid( generatedUuid );
			user.setResetPassword( true );
		}
		user.setName( userForm.getName() );
		user.setSurname( userForm.getSurname() );
		user.setEmail( userForm.getEmail() );
		user.setUserRol( userForm.isAdmin() ? UserRol.ADMIN : UserRol.USER );

		return userDao.save( user );
	}

	@Override
	@Transactional
	public TurnosResponse deleteUser( String sessionId, String uuid, String id ) {

		checkAuthenticationCredentials( sessionId, uuid );
		checkUserAdminLevel();

		TurnosResponse turnosResponse = new TurnosResponse();

		return turnosResponse;
	}
}
